//
//  JXPictureView.swift
//  swift高仿微博
//
//  Created by peijiaxing on 16/3/2.
//  Copyright © 2016年 peijiaxing. All rights reserved.
//

import UIKit
import SDWebImage


let ItemMargin : CGFloat = 10

let ItemWH = (UIScreen.mainScreen().bounds.width - 4 * ItemMargin) / 3

let JXPictureVCViewReuserID = "JXPictureViewCollectionViewReuserIdentifer"

class JXPictureView: UICollectionView,UICollectionViewDataSource {

    var pictureViewModel: JXStatusViewModel?{
        didSet{
            // 更新配图约束
            
            self.snp_updateConstraints { (make) -> Void in
                
                
                let size = self.calPictureSize()
                make.width.equalTo(size.width)
                make.height.equalTo(size.height)

        }
            reloadData()
    }
}
    var pictureList: [JXPictureViewModel]?{
        
        didSet{
            // 更新配图约束
            self.snp_updateConstraints {(make) -> Void in
                
                
                let size = self.calPictureSize()
                make.width.equalTo(size.width)
                make.height.equalTo(size.height)
                
            }
            reloadData()
        }
    }
    
    
    // 传递数据
   var FlowLayout: UICollectionViewFlowLayout = UICollectionViewFlowLayout()
    
    override init(frame: CGRect, collectionViewLayout layout: UICollectionViewLayout) {
        // 设置一个流水布局
        super.init(frame: frame, collectionViewLayout: FlowLayout)
        // 给 collectionView 一个透明颜色
        backgroundColor = UIColor.clearColor()
        
        setupUI()
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    private func setupUI(){
        
        self.dataSource = self
        // 注册一个cell
        self.registerClass(JXPictureViewControllerCell.self, forCellWithReuseIdentifier: JXPictureVCViewReuserID)
        
        // 设置水平
//        FlowLayout.scrollDirection = UICollectionViewScrollDirection.Horizontal
////
        // 设置cell 大小
        FlowLayout.itemSize = CGSizeMake(ItemWH, ItemWH)
    
        FlowLayout.minimumInteritemSpacing = ItemMargin
        FlowLayout.minimumLineSpacing = ItemMargin
        
    
    }
    
    override func layoutSubviews() {
        super.layoutSubviews()
        
        
    }
    // 计算配图宽高
    private func calPictureSize() -> CGSize{
        
        let count = pictureList?.count
        
        print(count)
        
        if count <= 3{
            
            //宽 = itemW * 3 + 2 * itemMargin ,
            //高 itemH
            return CGSizeMake(ItemWH * 3 + 2 * ItemMargin, ItemWH)
            
        } else if count == 4 {
            //4张图片, 宽 = itmeW * 2 + itemMargin 高 = 宽
            return CGSizeMake(ItemWH  * 2 + ItemMargin, ItemWH * 2 + ItemMargin)
            
        } else {
           
            let row = CGFloat((count! - 1)/3 + 1)
            
            return CGSizeMake(UIScreen.mainScreen().bounds.size.width - 2 * ItemMargin, (ItemMargin + ItemWH)*row)

            
        }
    }
}

// 加载数据源
extension JXPictureView{
    func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        return pictureList?.count ?? 0
    }
    
    func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
        
     
        
        let cell = collectionView.dequeueReusableCellWithReuseIdentifier(JXPictureVCViewReuserID, forIndexPath: indexPath) as! JXPictureViewControllerCell
       
        cell.backgroundColor = UIColor.clearColor()
        
        cell.pictureViewModel = pictureList![indexPath.item]
        
        
        return cell
    }
}

class JXPictureViewControllerCell : UICollectionViewCell{
    
    var pictureViewModel: JXPictureViewModel?{
        
        didSet{
        
        let url = NSURL(string:
            self.pictureViewModel?.model?.thumbnail_pic ?? "")
        
        if let Ourl = url {
            
            iconImageView.sd_setImageWithURL(Ourl)
        }
            
        }
        
    }
    
    
    // 数据传值
    override init(frame: CGRect) {
        super.init(frame: frame)
        
        setupUI()
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    private func setupUI(){
        
        contentView.addSubview(iconImageView)
        
        iconImageView.frame = self.bounds
        
    }
    
    private lazy var iconImageView: UIImageView = {
       
        let img = UIImageView()
        
        img.contentMode = UIViewContentMode.ScaleAspectFit
        
        img.clipsToBounds = true
        
        return img
        
    }()
}






